/**
 * Copyright &copy; 2012-2013 <a href="https://github.com/slacrey/jeesite">JeeSite</a> All rights reserved.
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.novunda.platform.common.utils;

import com.novunda.platform.common.context.SpringContextHolder;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;

/**
 * Cache工具类
 *
 * @author slacrey
 * @version 2013-5-29
 */
public final class CacheUtils {

    private CacheUtils() {
    }

    private static final CacheManager CACHE_MANAGER = SpringContextHolder.getBean(CacheManager.class);

    private static final String LOCAL_CACHE = "localCache";

    public static Object getObject(String key) {
        return getObject(LOCAL_CACHE, key);
    }

    public static void putObject(String key, Object value) {
        putObject(LOCAL_CACHE, key, value);
    }

    public static void remove(String key) {
        remove(LOCAL_CACHE, key);
    }

    public static Object getObject(String cacheName, String key) {
        Cache.ValueWrapper element = getCache(cacheName).get(key);
        return element == null ? null : element.get();
    }

    public static void putObject(String cacheName, String key, Object value) {
        getCache(cacheName).put(key, value);
    }

    public static void remove(String cacheName, String key) {
        getCache(cacheName).evict(key);
    }

    public static void clear(String cacheName) {
        getCache(cacheName).clear();
    }

    public static void clear() {
        clear(LOCAL_CACHE);
    }

    private static Cache getCache(String cacheName) {
        return getCacheManager().getCache(cacheName);
    }

    public static CacheManager getCacheManager() {
        return CACHE_MANAGER;
    }

}
